package com.snimay.common;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.snimay.util.CurrentUserUtils;

/**   
 * @Title      : SessionInterceptor.java
 * @Package    : com.snimay.common
 * @Description: 登录拦截 
 * @author     : xxy
 * @date       : 2018年4月28日 上午11:43:33
 * @version    : V1.0   
 */
@Component
//@Scope("session")
public class SessionInterceptor implements HandlerInterceptor {
	
	
	private int i = 0;
	
	/*@Autowired
	RedisService redisService;*/
	/**
	 * @Description: TODO
	 * @author     : xxy
	 * @param request
	 * @param response
	 * @param o
	 * @return
	 * @throws Exception
	 * @throws
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
		String path = request.getServletPath();
		CurrentUserUtils currentUserUtils = CurrentUserUtils.getInstance();
		++i;
		System.out.println(i);
		System.out.println(path);
		//System.out.println("================1===================");
		
		  long startTime = System.currentTimeMillis();
	        request.setAttribute("startTime", startTime);
	        if (o instanceof HandlerMethod) {
	            StringBuilder sb = new StringBuilder(1000);

	            sb.append("-----------------------").append(new Date().toString())
	            .append("-------------------------------------\n");
	            HandlerMethod h = (HandlerMethod) o;
	            sb.append("Controller: ").append(h.getBean().getClass().getName()).append("\n");
	            sb.append("Method    : ").append(h.getMethod().getName()).append("\n");
	            sb.append("Params    : ").append(getParamString(request.getParameterMap())).append("\n");
	            sb.append("URI       : ").append(request.getRequestURI()).append("\n");
	            System.out.println(sb.toString());
	        }
	        return true;
		
	}

	/**
	 * @Description: TODO
	 * @author     : xxy
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param o
	 * @param modelAndView
	 * @throws Exception
	 * @throws
	 */
	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
	       ModelAndView modelAndView) throws Exception {
		
		//System.out.println("================2===================");
		
		long startTime = (Long) httpServletRequest.getAttribute("startTime");
        long endTime = System.currentTimeMillis();
        long executeTime = endTime - startTime;
        if(o instanceof HandlerMethod){
            StringBuilder sb = new StringBuilder(1000);
            sb.append("CostTime  : ").append(executeTime).append("ms").append("\n");
            sb.append("-------------------------------------------------------------------------------");
            System.out.println(sb.toString());
        }
		
	}
   private String getParamString(Map<String, String[]> map) {
        StringBuilder sb = new StringBuilder();
        for(Entry<String,String[]> e:map.entrySet()){
            sb.append(e.getKey()).append("=");
            String[] value = e.getValue();
            if(value != null && value.length == 1){
                sb.append(value[0]).append("\t");
            }else{
                sb.append(Arrays.toString(value)).append("\t");
            }
        }
        return sb.toString();
    }

	
    /**
	 * @Description: TODO
	 * @author     : xxy
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param o
	 * @param e
	 * @throws Exception
	 * @throws
	 */
	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
           Object o, Exception e) throws Exception {
		//System.out.println("================3===================");

   }

   public String getRemortIP(HttpServletRequest request) {
       if (request.getHeader("x-forwarded-for") == null) {
           return request.getRemoteAddr();
       }
       return request.getHeader("x-forwarded-for");
   }
}
